<?php
namespace app\np\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use think\Db;
use think\Config;
class Timeout extends Command
{
    protected function configure()
    {
        $this->setName('timeout')->setDescription('cover every Minutes ');

    }
    protected function execute(Input $input, Output $output)
    {



        $out_time= Config::get("baseextend.out_time");//失效时间

        //订单流操作
        $UsersInquiryModel= new \app\np\model\UsersInquiry();//订单详情 前台订单卖出操作
        $UsersAskpriceModel= new \app\np\model\UsersAskprice();//订单详情 前台订单卖出操作
        $UsersInquiryResult =  $UsersInquiryModel->alias("a")
         ->field("a.*,sp.confirm_time")
        ->join("users_askprice sp","sp.inquir_id=a.id","left")
        ->where("sp.status=0 or (sp.status is null)")->limit(5)->order("a.id asc")->select();
        if(!empty($UsersInquiryResult)){
            $UsersInquiryResult = json_decode(json_encode($UsersInquiryResult),true);
            $c_time = time();
            foreach ($UsersInquiryResult as $k=>$v){
                $askTime = $v['ask_time'];
                $cofireTime  = bcadd($out_time,$askTime);
                if($c_time>$cofireTime){
                    //订单过期处理
                    $UsersInquiryModel->delete(['id'=>$v['id']]);

                }


            }

        }






        //订单流操作
        //已回执的处理
        $UsersInquiryResult =  $UsersInquiryModel->alias("a")
            ->field("a.*,sp.confirm_time")
            ->join("users_askprice sp","sp.inquir_id=a.id","left")
            ->where("sp.status=1")->limit(5)->order("sp.confirm_time asc")->select();
        if(!empty($UsersInquiryResult)){
            $UsersInquiryResult = json_decode(json_encode($UsersInquiryResult),true);
            $c_time = time();
            foreach ($UsersInquiryResult as $k=>$v){
                if(!empty($v['confirm_time'])){
                    $cofireTime  = bcadd($out_time,$v['confirm_time']);
                    if($c_time>$cofireTime){
                        //订单过期处理
                        $UsersAskpriceModel->save(['status'=>4],['inquir_id'=>$v['id']]);

                    }
                }

            }

        }







    }
}